package com.vgulu.server.login.controller;

import com.vgulu.server.common.TConstant;
import com.vgulu.server.common.bean.VResponse;
import com.vgulu.server.common.exception.VBaseException;
import com.vgulu.server.login.bean.LoginBean;
import com.vgulu.server.login.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 注册
 * Created by wangchunsee on 16/10/4.
 */
@Controller
@RequestMapping("/pass")
public class RegisterController {
    private static final Logger logger = LoggerFactory.getLogger(RegisterController.class);
    @Autowired
    UserService userService;


    @RequestMapping("/emailRegister")
    @ResponseBody
    public VResponse<LoginBean> emailRegister(@RequestParam(required = true) String appId, @RequestParam(required = true) String email
            , @RequestParam(required = true)String pwd,@RequestParam(required = true)String channel){
        try{
            LoginBean loginBean = userService.emailRegister(appId,email,pwd,channel);
            return VResponse.success(loginBean);
        }catch (VBaseException e){
            logger.error(VBaseException.TAG,e);
            return VResponse.error(e);
        }catch (Throwable e){
            logger.error(TConstant.LOGTAG_UNCATCH_EXCEPTION,e);
            return VResponse.error(e);
        }
    }

    @RequestMapping("/mobileRegister")
    @ResponseBody
    public VResponse<LoginBean> mobileRegister(@RequestParam(required = true) String appId, @RequestParam(required = true) String mobile
            , @RequestParam(required = true)String pwd,@RequestParam(required = true)String smsCode,@RequestParam(required = true)String channel){
        try{
            LoginBean loginBean = userService.mobileRegister(appId,mobile,pwd,smsCode,channel);
            return VResponse.success(loginBean);
        }catch (VBaseException e){
            logger.error(VBaseException.TAG,e);
            return VResponse.error(e);
        }catch (Throwable e){
            logger.error(TConstant.LOGTAG_UNCATCH_EXCEPTION,e);
            return VResponse.error(e);
        }
    }
}
